<script type="text/javascript">
    $(document).ready(function() {
        $("#e1").select2({
            placeholder: "กรุณาเลือก..."
            , allowClear: true
        });
        $("#debtor_n2").hide();

        $("#debtor_group_id").change(function() {
            //alert($(this).val());
            if ($(this).val() == 7) {
                $("#debtor_name").attr("disabled", true);
                $("#debtor_n1").hide();
                $("#debtor_n2").show();
                $("#refference_id").attr("readonly", true);

            } else {
                $("#debtor_name").attr("disabled", false);
                $("#debtor_n1").show();
                $("#debtor_n2").hide();
                $("#refference_id").attr("readonly", false);
            }

        });

        $("#e1").change(function() {
            //alert($(this).val());
            $("#refference_id").attr("readonly", true);
            $("#refference_id").val($(this).val());
            
        });

        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
            submitSuccess: function($form, event) {
                event.preventDefault();
                //alert("success");

                $.ajax({
                    type: "POST",
                    url: "debtor_process.php",
                    data: $('#frm_main').serialize(),
                    cach: false,
                    error: function() {
                        alert("ERROR !!!!");
                    },
                    success: function(data) {

//                        if (confirm("คุณต้องการพิมพ์ฎีกาหรือไม่")) {
//                            window.location.href = "index.php?pages=v_budget";
//                        } else {
//                            window.location.href = "index.php?pages=v_budget";
//                        }
                        //$("#dispStatus").html(data);
                        //alert(data);
                        window.location.href = "index.php?pages=debtor";

                    }
                });
            }
        });
    });


</script>
<?php
$debtor_id = $_REQUEST["debtor_id"];

$db = new myDBS();

if ($debtor_id) {
    $sql = "select * from tbl_debtor where debtor_id='$debtor_id'";
    $row = $db->fetch_object($db->runSQL($sql));
}
?>

<div id="dispStatus"></div>
<form class="form-horizontal" id="frm_main" name="frm_main" method="post">

    <input type="hidden" id="task" name="task" value="<?php echo ($debtor_id) ? "edit" : "new"; ?>">
    <input type="hidden" id="debtor_id" name="debtor_id" value="<?php echo $debtor_id; ?>">
    <div class="panel panel-info">
        <div class="panel-heading">ลูกหนี้</div>
        <div class="panel-body">

            <div class="control-group">
                <label class="control-label" for="ชื่อลูกหนี้">ชื่อลูกหนี้</label>
                <div class="controls" id="debtor_n1">
                    <input type="text" id="debtor_name" name="debtor_name" value="<?php echo $row->debtor_name; ?>">
                </div>

                <div class="controls" id="debtor_n2">
                    <select  id="e1" name="e1" style="width: 250px;">
                        <!--<option value="1" selected>1111</option>-->
                        <?php
                        $db->selectDB("hr_udoncity");

                        $sql = "select * from person ";

                        $result = $db->runSQL($sql);
                        while ($row2 = mysql_fetch_object($result)) {
                            ?>
                            <option value="<?php echo $row2->id; ?>" <?php echo ($row1->card_id3 == $row2->id) ? " selected" : ""; ?>><?php echo $row2->fname . "  " . $row2->lname; ?></option>
                            <?php
                        }

                        $db->selectDB("account");
                        ?>
                    </select>                    
                </div>


            </div>

            <div class="control-group">
                <label class="control-label" for="กลุ่มลูกหนี้">กลุ่มลูกหนี้</label>
                <div class="controls">
                    <select id="debtor_group_id" name="debtor_group_id">
                        <option value=""></option>
                        <?php
                        $sql = "SELECT * FROM tbl_debtor_group";
                        $t_query = $db->runSQL($sql);

                        while ($row1 = mysql_fetch_object($t_query)) {
                            echo "<option value='$row1->id' ";
                            echo ($row1->id == $row->debtor_group_id) ? "selected" : "";
                            echo ">$row1->group_name</option>";
                        }
                        ?>
                    </select>

                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="เลขที่บัตรประชาชน">เลขที่บัตรประชาชน</label>
                <div class="controls">
                    <input type="text" id="card_id" name="card_id" value="<?php echo $row->card_id; ?>">
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="ที่อยู๋">ที่อยู่</label>
                <div class="controls">
                    <input type="text" id="addr" name="addr" value="<?php echo $row->addr; ?>">
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="โทรศัพท์">โทรศัพท์</label>
                <div class="controls">
                    <input type="text" id="tel" name="tel" value="<?php echo $row->tel; ?>">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="รหัสอ้างอิง">รหัสอ้างอิง</label>
                <div class="controls">
                    <input type="text" id="refference_id" name="refference_id" value="<?php echo $row->refference_id; ?>">
                </div>
            </div>

            <div class="control-group">
                <div class="controls">

                    <button type="submit" class="btn btn-info"><i class="icon-ok"></i> บันทึกข้อมูล</button>&nbsp;&nbsp;&nbsp;<a href="index.php?pages=debtor" class="btn" ><i class="icon-remove"></i> ยกเลิก</a>
                </div>
            </div>
        </div>
    </div>
</form>


<?php ?>
